<?php
class Zen_View_Helper_Color_Widget extends Zend_View_Helper_FormElement {
	
	public function color_Widget($name, $value = null, $attribs = null)  {
		$info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable

        // build the element
        $disabled = '';
        if ($disable) {
            // disabled
            $disabled = ' disabled="disabled"';
        }

        // XHTML or HTML end tag?
        $endTag = ' />';
        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
            $endTag= '>';
        }
		
        $id = $this->view->escape($id);
        
        Zen_Script::add('/cms/library/Zen/View/Helper/Color/js/colorpicker.js');
        Zen_Style::add ('/cms/library/Zen/View/Helper/Color/css/colorpicker.css');
        
        $xhtml = '<input type="text"'
                . ' name="' . $this->view->escape($name) . '"'
                . ' id="' . $id . '"'
                . ' value="' . $this->view->escape($value) . '"'
                . $disabled
                . $this->_htmlAttribs($attribs)
                . $endTag;

        $js = " <script type='text/javascript'>
				$('#$id').ColorPicker({
					onSubmit: function(hsb, hex, rgb, el) {
						$(el).val(hex);
						$(el).ColorPickerHide();
					},
					onBeforeShow: function () {
						$(this).ColorPickerSetColor(this.value);
					}
				});
				</script>
        "; 
        
        return  $xhtml.$js;
	}
	
} 